Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscription oauth v2 #1033

Open
wants to merge 188 commits into
base: main
Choose a base branch
from

Conversation

federicocappelli
Copy link
Member

@federicocappelli federicocappelli commented Oct 25, 2024

Task/Issue URL: https://app.asana.com/0/1205842942115003/1207991044706235/f
iOS PR: duckduckgo/iOS#3480
macOS PR: duckduckgo/macos-browser#3580
What kind of version bump will this require?: Major
CC: @miasma13

iOS PR: duckduckgo/iOS#3480
macOS PR: duckduckgo/macos-browser#3580

Description:

This PR introduces the use of OAuth V2 authentication in Privacy Pro Subscription.
The code changes are comprehensive due to the paradigm changes between the old access token lifecycle and the new JWT lifecycle.
The Subscription UI and UX should be unchanged.

Steps to test this PR:
Test all Privacy Pro Subscription features and UX, more details here


Internal references:

Software Engineering Expectations
Technical Design Template

Copy link
Contributor

@miasma13 miasma13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second part of the review

Sources/Networking/OAuth/OAuthClient.swift Outdated Show resolved Hide resolved
Comment on lines 22 to 26
public final class SessionDelegate: NSObject, URLSessionTaskDelegate {

/// Disable automatic redirection, in our specific OAuth implementation we manage the redirection, not the user
public func urlSession(_ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest) async -> URLRequest? {
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it is used only in one test. Please confirm if this is relevant and move accordingly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the entire SessionDelegate? no, it's used in the OAuthClient init in the main apps

let urlSession = URLSession(configuration: configuration,
                                    delegate: SessionDelegate(),
                                    delegateQueue: nil)

}

/// The sole entity responsible of obtaining, storing and refreshing an OAuth Token
public protocol SubscriptionTokenProvider {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a big file I would split out these additional protocols and enums not directly related to SubscriptionManager to separate files for improved readability.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved SubscriptionFeature and SubscriptionTokenProvider to dedicated files

Comment on lines 418 to 424
public func isFeatureActive(_ entitlement: SubscriptionEntitlement) async -> Bool {
guard isUserAuthenticated else { return false }

let currentFeatures = await currentSubscriptionFeatures(forceRefresh: false)
return currentFeatures.contains { feature in
feature.entitlement == entitlement && feature.enabled
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this naming confusing. Let's go one by one, currentSubscriptionFeatures above is clear and well defined, features that are provided by current subscription. But when you move to isFeatureActive and SubscriptionFeature.enabled it can be interpreted in multiple ways (e.g. isFeatureActive(.networkProtection), does it mean that VPN is running? etc.).

  • isFeatureActive - means if the feature is provided within the current subscription and if current user has entitlements to use it, previously we sticked to established vocabulary and checked if used has entitlements to a feature. If you don't like this maybe something along the line isFeatureAvailableForUser?
  • SubscriptionFeature.enabled - this is the underlying property storing Bool for the above so I would align them together

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree:

  • isFeatureActive renamed isFeatureAvailableForUser
  • SubscriptionFeature.enabled renamed availableForUser

cacheSerialQueue.sync {
subscriptionCache.reset()
}
// NotificationCenter.default.post(name: .subscriptionDidChange, object: self, userInfo: nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please double check if this is needed.

Copy link
Member Author

@federicocappelli federicocappelli Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed for now, anything related to the subscription cancellation is handled by the signOut that sends the proper notification. I'm keeping this commented to remind me where it COULD be if I need it, not to be merged anyway.

# Conflicts:
#	Sources/PrivacyDashboard/PrivacyDashboardUserScript.swift
…i/subscription_oauth_api_v2

# Conflicts:
#	Sources/Common/Extensions/DateExtension.swift
#	Sources/MaliciousSiteProtection/API/APIClient.swift
#	Sources/Networking/v2/APIRequestV2.swift
#	Sources/NetworkingTestingUtils/MockLegacyTokenStorage.swift
#	Sources/NetworkingTestingUtils/MockOAuthClient.swift
#	Sources/NetworkingTestingUtils/MockTokenStorage.swift
#	Tests/NetworkingTests/v2/APIRequestV2Tests.swift
#	Tests/NetworkingTests/v2/APIServiceTests.swift
#	Tests/NetworkingTests/v2/Extensions/DictionaryURLQueryItemsTests.swift
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants